package com.lenovo.smarttraffic.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.util.DataUtil_7;

import java.util.ArrayList;
import java.util.List;

public class Activity_7 extends AppCompatActivity {
    private ImageView icon;
    private TextView title;
    private TextView ditu;
    private EditText a_7_ed1;
    private TextView a_7_tv1;
    private LinearLayout a_7_lin1;
    private ListView a_7_lv1;
    private TextView a_7_tv2;
    List<String> list = new ArrayList<>();
    MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_7);
        initView();

        list = DataUtil_7.strings;

        adapter = new MyAdapter();

        a_7_lv1.setAdapter(adapter);


    }

    private void initView() {
        icon = (ImageView) findViewById(R.id.icon);
        title = (TextView) findViewById(R.id.title);
        ditu = (TextView) findViewById(R.id.ditu);
        a_7_ed1 = (EditText) findViewById(R.id.a_7_ed1);
        a_7_tv1 = (TextView) findViewById(R.id.a_7_tv1);
        a_7_lin1 = (LinearLayout) findViewById(R.id.a_7_lin1);
        a_7_lv1 = (ListView) findViewById(R.id.a_7_lv1);
        a_7_tv2 = (TextView) findViewById(R.id.a_7_tv2);

        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        title.setText("实时交通");

        ditu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Activity_7.this, Activity_6.class));
            }
        });

        a_7_tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ed1 = a_7_ed1.getText().toString().trim();
                if (TextUtils.isEmpty(ed1)) {
                    Toast.makeText(Activity_7.this, "请输入公交车路线", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.parseInt(ed1.trim())>8) {
                    Toast.makeText(Activity_7.this, "无此路线！", Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(Activity_7.this, Activity_7_2.class).putExtra("position", ed1));
            }
        });

        a_7_tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.clear();
                DataUtil_7.strings.clear();
                adapter.notifyDataSetChanged();
            }
        });

    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("NewApi")
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = getLayoutInflater().inflate(R.layout.a_7_lv, null);


            TextView a_7_lv_tv1 = (TextView) view.findViewById(R.id.a_7_lv_tv1);

            a_7_lv_tv1.setText(list.get(i));

            return view;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }
}
